﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using OtherDevice;
using System.Diagnostics;
using CommonTools;
using System.Threading;
using MotionIoLib;
using Newtonsoft.Json;
namespace ModuleUnit
{
    public class Config_Screw
    {
        [Category("工站参数"), DisplayName("1.螺丝机 "), Description("螺丝机 ")]
        public string str_螺丝机 { set; get; } = "";
        [Category("工站参数"), DisplayName("2.测高仪"), Description("测高仪")]
        public string str_测高仪 { set; get; } = "";
        [Category("工站参数"), DisplayName("3.螺丝准备好信号"), Description("螺丝准备好信号")]
        public string str_螺丝准备好信号 { set; get; } = "";
        [Category("工站参数"), DisplayName("4.电批吸真空"), Description("电批吸真空")]
        public string str_电批吸真空 { set; get; } = "";
        [Category("工站参数"), DisplayName("5.电批破真空"), Description("电批破真空")]
        public string str_电批破真空 { set; get; } = "";
        [Category("工站参数"), DisplayName("6.电批升降气缸"), Description("电批升降气缸")]
        public string str_电批升降气缸 { set; get; } = "";
        [Category("工站参数"), DisplayName("7.电批真空感应信号"), Description("电批真空感应信号")]
        public string str_电批真空感应信号 { set; get; } = "";
        [Category("工站参数"), DisplayName("8.螺丝机启动"), Description("螺丝机启动")]
        public string str_电批启动 { set; get; } = "";
        [Category("工站参数"), DisplayName("9.电批运行中"), Description("电批运行中")]
        public string str_电批运行中 { set; get; } = "";
        [Category("工站参数"), DisplayName("10.电批OK"), Description("电批OK")]
        public string str_电批OK { set; get; } = "";
        [Category("工站参数"), DisplayName("11.电批NG"), Description("电批NG")]
        public string str_电批NG { set; get; } = "";
        [Category("工站参数"), DisplayName("12.程序0"), Description("程序0")]
        public string str_程序0 { set; get; } = "";
        [Category("工站参数"), DisplayName("13.程序1"), Description("程序1")]
        public string str_程序1 { set; get; } = "";
        [Category("工站参数"), DisplayName("14.程序2"), Description("程序2")]
        public string str_程序2 { set; get; } = "";
        [Category("工站参数"), DisplayName("14.程序3"), Description("程序3")]
        public string str_程序3 { set; get; } = "";
        [Category("工站参数"), DisplayName("15.滤波时长"), Description("滤波时长")]
        public int n_滤波时长 { set; get; } = 20;
    }
    [Description("螺丝机模块")]
    public class Module_Screw: ModuleBase
    {
        public Config_Screw config = null;
        public Module_Screw(object config, string strName) : base( strName)
        {
            CommonDlg.AlarmInfoMannage.GetInstance().AddAlarmInfo((int)ModuleAlarm.电批吸真空, "电批吸真空感应");

            if (config == null)
            {
                this.config = new Config_Screw();
            }
            else
            {
                this.config = JsonConvert.DeserializeObject<Config_Screw>(config.ToString());

            }
            this.obj_Config = this.config;
        }

        LaserBase Device_测高
        {
            set { }
            get
            {
                return DeviceMgr.GetInstance().Function_获取串口(config.str_测高仪) as LaserBase;
            }
        }
        Screw_Base Device_螺丝机
        {
            set { }
            get
            {
                return DeviceMgr.GetInstance().Function_获取网口(config.str_螺丝机) as Screw_Base;
            }
        }
        ClylinerBase clyLiner_电批升降
        {
            set { }
            get
            {
                return DeviceMgr.GetInstance().Function_获取气缸(config.str_电批升降气缸);
            }
        }
        public bool Function_Reset()
        {
            config.str_电批吸真空.SetIO(false);
            config.str_电批破真空.SetIO(false);
            config.str_电批启动.SetIO(false);
            clyLiner_电批升降?.Function_后退(true);
            return true;
        }
        public bool Function_是否已取螺丝()
        {
            return config.str_电批真空感应信号.GetInput();
        }
        public bool Function_等待螺丝机备妥(int n_TimeOut=3000)
        {
            if(sys.g_AppMode==BaseDll.AppMode.Run)
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                while (!config.str_螺丝准备好信号.GetInput())
                {
                    if (stopwatch.ElapsedMilliseconds > 3000)
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        public bool Function_取螺丝(string str_取料程序IO号="")
        {
            if (str_取料程序IO号 == "")
            {
                if (sys.g_AppMode == BaseDll.AppMode.Run)
                    Io_置位并检查信号到位(config.str_电批吸真空, true, config.str_电批真空感应信号, true, config.str_电批真空感应信号, 3000);
                else
                {
                    config.str_电批吸真空.SetIO(true);
                    return true;
                }
                Thread.Sleep(300);
            }
            else
            {
                config.str_电批吸真空.SetIO(true);
                str_取料程序IO号.SetIO(true);
                Thread.Sleep(100);
                config.str_电批启动.SetIO(true);
                clyLiner_电批升降?.Function_前进(true);
                Thread.Sleep(300);
                config.str_电批启动.SetIO(false);
                str_取料程序IO号.SetIO(false);
                clyLiner_电批升降?.Function_前进(false);
                if (sys.g_AppMode == BaseDll.AppMode.Run)
                    Io_置位并检查信号到位(config.str_电批吸真空, true, config.str_电批真空感应信号, true, config.str_电批真空感应信号, 3000);
                else
                {
                    config.str_电批吸真空.SetIO(true);
                    return true;
                }
            }
            return config.str_电批真空感应信号.GetInput();

        }     
        public bool Function_打螺丝(int str_锁附程序号,ref double d_Torque,ref double d_Angle,ref double d_High,Action action_ZUp=null)
        {
            clyLiner_电批升降.Function_前进(true);
            config.str_电批吸真空.SetIO(false);
            double d_Value = 0;
            config.str_电批启动.SetIO(false);
            config.str_程序0?.SetIO((str_锁附程序号 & 0b00000001) == 0b00000001);
            config.str_程序1?.SetIO((str_锁附程序号 & 0b00000010) == 0b00000010);
            config.str_程序2?.SetIO((str_锁附程序号 & 0b00000100) == 0b00000100);
            config.str_程序3?.SetIO((str_锁附程序号 & 0b00001000) == 0b00001000);
            config.str_电批启动.SetIO(true);
            Thread.Sleep(1000);
            int i = 0;
            while (true)
            {
                if (IOMgr.GetInstace().Function_ReCheckIO(config.str_电批运行中, true, config.n_滤波时长))
                    break;
                Thread.Sleep(10);
                if (i > 3000)
                {
                    return false;
                }
                i++;
            }
            config.str_程序0?.SetIO(false);
            config.str_程序1?.SetIO(false);
            config.str_程序2?.SetIO(false);
            config.str_程序3?.SetIO(false);
            config.str_电批启动.SetIO(false);
            int n_Time = 0;
            //获取扭力
            bool b_Result = !config.str_电批NG.GetInput() && config.str_电批OK.GetInput();
            double d_TorqueValue = 0;
            double d_AngleValue = 0;
            Task task_GetData = Task.Run(() =>
            {
                 Retry:
                Device_螺丝机?.Function_GetScrewTorque(out d_TorqueValue);
                  Device_螺丝机?.Function_GetScrewAngle(out d_AngleValue);
                  if (d_TorqueValue == 0 || d_AngleValue == 0)
                  {
                      if (n_Time < 3)
                      {
                          n_Time++;
                          Thread.Sleep(300);
                          goto Retry;
                      }
                  }

              });
            Device_测高?.Get(1, 100, ref d_Value);
            d_High = d_Value;
            clyLiner_电批升降.Function_后退(false);
            if (action_ZUp != null)
                action_ZUp();
            clyLiner_电批升降.Function_后退(true);
            Task.WaitAll(task_GetData);
            d_Torque = d_TorqueValue;
            d_Angle = d_AngleValue;
            return b_Result;
        }
        public bool Function_抛螺丝()
        {
            config.str_电批吸真空.SetIO(false);
            config.str_电批破真空.SetIO(true);
            Thread.Sleep(500);
            config.str_电批破真空.SetIO(false);
            return true;
        }



    }
}